package com.allwees.bs.module.oms.req.query;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.allwees.bs.core.modelbase.req.QueryReq;
import com.allwees.bs.module.oms.entity.OmsOrder;
import com.allwees.core.common.constant.EStatus;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = true)
public class OmsOrderQueryReq extends QueryReq<OmsOrder> {

    /**
     * 订单号
     */
    private String no;
    /**
     * CODE
     */
    private String code;
    /**
     * 开始查询时间
     */
    private Date createdAtFrom;

    /**
     * 查询截至时间
     */
    private Date createdAtTo;



    /**
     * 买家ID
     */
    private String userUuid;

    /**
     * 支付方式
     *
     */
    private String payType;
    /**
     * 订单来源
     */
    private String source;

    /**
     * 订单状态
     */
    private EOmsOrderStatus status;

    public final void buildQuery(QueryWrapper<OmsOrder> queryWrapper) {
        queryWrapper.eq(StringUtils.isNotBlank(code),"promo_code",code)
                .eq(StringUtils.isNotBlank(source),"platform",source)
                .eq(StringUtils.isNotBlank(payType),"payment_type",payType)
                .eq(StringUtils.isNotBlank(userUuid),"user_uuid",userUuid)
                .eq(StringUtils.isNotBlank(no), "no", no)
                .ge( createdAtFrom != null, "created_at", createdAtFrom)
                .le(createdAtTo != null, "created_at", createdAtTo)
                .in(null != status ,"status",status != null? status.entityStatus():null);

    }


    private enum EOmsOrderStatus {
        ON_CREATE {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(
                        EStatus.CREATED.value()
                );
            }
        },
        WAIT_AUDIT {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(
                        EStatus.WAIT_AUDIT.value()
                );
            }
        },
        AUDIT_REJECTED {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(
                        EStatus.AUDIT_REJECTED.value()
                );
            }
        },
        WAIT_PUTON {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(
                        EStatus.WAIT_PUTON.value(),
                        EStatus.AUDIT_APPROVED.value()
                );
            }
        },
        ON_PUTOFF {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(
                        EStatus.PUTOFF_ONESELF.value(),
                        EStatus.PUTOFF_QA_FAILURE.value(),
                        EStatus.PUTOFF_OTHERS.value(),
                        EStatus.PUTOFF_SELLOUT.value(),
                        EStatus.PUTOFF_EDITING.value()
                );
            }
        },
        WAIT_MERCHANT_SHIPPING{
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(
                        EStatus.OMS_WAIT_MERCHANT_SHIP.value()
                );
            }
        },
        MERCHANT_SHIPPING {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(
                        EStatus.OMS_SHIPPED.value()
                );

            }
        },
        ON_SALE {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(EStatus.NORMAL.value());
            }
        };

        abstract List<Integer> entityStatus();
    }
}
